package com.github.live.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.widget.TextView;

import com.github.live.R;
import com.github.live.base.BookingManager;
import com.github.live.manager.ChannelsManager;
import com.github.live.component.MarqueeTextView;
import com.github.live.manager.LiveInitializer;
import com.github.livebase.bean.EpgInformation;
import com.github.livebase.util.base.DateUtil;

import java.util.List;

/**
 * @ClassName EpgHeaderAdapter
 * @description: epg回看界面详细信息，时间 节目 状态
 * @author: biao532
 * @create: 2024-01-03 16:56
 * @Version 1.0
 **/
public class EpgBodyAdapter extends SimpleRecyclerAdapter<EpgInformation>{
    private int selectedChannelIndex = -1;
    private int activeChannelIndex = -1;
    @SuppressLint("ResourceType")
    public EpgBodyAdapter() {
        super(R.layout.item_replay_epg_body);
    }

    @Override
    void onBindViewHolder(SimpleViewHolder holder, EpgInformation data, int position) {
        TextView d = holder.findViewById(R.id.epg_time_start);
        MarqueeTextView dw = holder.findViewById(R.id.epg_title);
        TextView op = holder.findViewById(R.id.operation);
        d.setText(data.getStartString());
        dw.setText(data.getTitle());
        int color = Color.WHITE;
        if (position != selectedChannelIndex && position == activeChannelIndex) {
            color = mContext.getResources().getColor(R.color.color_theme);
        }

        d.setTextColor(color);
        dw.setTextColor(color);
        op.setTextColor(color);

        if (data == EpgInformation.NO_INFORMATION) {
            op.setText("");
            return;
        }
        long now = System.currentTimeMillis();
        if (DateUtil.betweenAnd(now, data.getStart(), data.getEnd())) {
            op.setText("直播中");
            return;
        }
        ChannelsManager cm = LiveInitializer.getInitializer().getChannelsManager();
        if (position == activeChannelIndex
                && !cm.isLive()
                && now > data.getEnd()) {
            op.setText("回看中");
            return;
        }

        if (now < data.getStart()) {
            if (BookingManager.singleton().isBooking(data)) {
                op.setText("已预约");
                op.setTextColor(mContext.getResources().getColor(R.color.color_FFB800));
            }
            else {
                op.setText("可预约");
            }
            return;
        }

        if (cm.getCurrentSource().supportedReplay()){
            op.setText("可回看");
        }
        else {
            op.setText("");
            //op.setText("不支持回看");
        }
    }

    public void setSelectedChannelIndex(int selectedChannelIndex) {
        if (selectedChannelIndex == this.selectedChannelIndex) return;
        int preSelectedChannelIndex = this.selectedChannelIndex;
        this.selectedChannelIndex = selectedChannelIndex;
        if (preSelectedChannelIndex != -1)
            notifyItemChanged(preSelectedChannelIndex);
        if (this.selectedChannelIndex != -1)
            notifyItemChanged(this.selectedChannelIndex);
        else if (this.activeChannelIndex != -1)
            notifyItemChanged(this.activeChannelIndex);
    }

    public void setActiveChannelIndex(int activeChannelIndex) {
        if (this.activeChannelIndex == activeChannelIndex) return;
        int preFocusedChannelIndex = this.activeChannelIndex;
        this.activeChannelIndex = activeChannelIndex;
        if (preFocusedChannelIndex != -1)
            notifyItemChanged(preFocusedChannelIndex);
        if (this.activeChannelIndex != -1)
            notifyItemChanged(this.activeChannelIndex);
    }

    public int getSelectedChannelIndex() {
        return selectedChannelIndex;
    }

    public int getActiveChannelIndex() {
        return activeChannelIndex;
    }

    @Override
    public void setData(List<EpgInformation> mData) {
        this.activeChannelIndex = -1;
        this.selectedChannelIndex = -1;
        super.setData(mData);
    }
}
